// 信息管理接口
import request from '@/utils/request'
import type { ToolResData } from './toolType'
import type { InfoResData,queryParams } from './infoType'

enum API {
  tool_tree = '/api/expand-learning/category/page',
  add_tool = '/api/expand-learning/item/add',
  update_tool = '/api/expand-learning/item/update',
  delete_tool = '/api/expand-learning/item/delete',
  getDataByCategoryCode = '/api/expand-learning/item/page'
}
// 获取工具列表
export const reqToolTreeData = () => {
  return request.get<any, ToolResData>(API.tool_tree)
}
// 新增信息、编辑信息
export const reqAddOrUpdateInfo = (data: any) => {
  let hasId = data.has('id')
  let idValue = hasId ? data.get('id') : null
  let hasValue = idValue !== null && idValue !== ''
  if (hasValue) {
    return request.put<any, any>(API.update_tool, data)
  } else {
    return request.post<any, any>(API.add_tool, data)
  }
}
// 删除单个信息
export const reqRemoveInfo = (id: number, version: number) => {
  return request.delete<any, any>(API.delete_tool + `?id=${id}&version=${version}`)
}
// 根据工具categoryCode查询信息表格数据
export const reqGetDataByCategoryCode = (params: queryParams) => {
  const queryParams = new URLSearchParams({
    current: params.current.toString(),
    size: params.size.toString(),
    categoryCode: params.categoryCode,
    itemTitle: params.itemTitle || ''
  }).toString()
  const url = `${API.getDataByCategoryCode}?${queryParams}`
  return request.get<any, InfoResData>(url)
}

